<?php
/**
 * Holds Configuration Data relevant to the Point of Sale.
 * Keys are additionally searched for in configuration table of the Zen Cart Database.
 * @copyright 2008 Nicolas Schudel
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
 */

class Configuration{
	
	private static $_instance = null;
	
	private $db;
	private $cache;
	private $database_tables;
	private $database_settings;
	private $pos_settings;

	public function __construct() {
		$this->createSettings();
		$this->db = ServiceLocator::getService( 'ConnectionFactory' );
		$this->cache = array();
		$this->connect();
	}
	
	protected function __clone() {
	  // restricts cloning of the object
	}

	private function createSettings() {
		
		$this->database_settings = array(
			'DB_SERVER' => 'localhost',
			'DB_SERVER_USERNAME' => 'root',
			'DB_SERVER_PASSWORD' => 'root',
			'DB_DATABASE' => 'zencart_138',
			'DB_USE_PCONNECT' => true
		);
		
		$this->pos_settings = array(
			'POS_DEFAULT_CUSTOMER' => 1,
			'POS_DEFAULT_LANGUAGE_ID' => 1,
			'POS_DEFAULT_ORDER_STATUS' => 3,
			'POS_DEFAULT_ORDER_COMMENTS' => 'Paymenty handled by ',
			'POS_DISPLAY_SALES_PRICE' => true
		);
		
		$this->database_tables = array(
			'TABLE_PREFIX' => '',
			'TABLE_ADDRESS_BOOK' => 'address_book',
			'TABLE_ADMIN' => 'admin',
			'TABLE_ADMIN_ACTIVITY_LOG' => 'admin_activity_log',
			'TABLE_ADDRESS_FORMAT' => 'address_format',
			'TABLE_AUTHORIZENET' => 'authorizenet',
			'TABLE_BANNERS' => 'banners',
			'TABLE_BANNERS_HISTORY' => 'banners_history',
			'TABLE_CATEGORIES' => 'categories',
			'TABLE_CATEGORIES_DESCRIPTION' => 'categories_description',
			'TABLE_CONFIGURATION' => 'configuration',
			'TABLE_CONFIGURATION_LANGUAGE' => 'configuration_language',
			'TABLE_CONFIGURATION_GROUP' => 'configuration_group',
			'TABLE_COUNTER' => 'counter',
			'TABLE_COUNTER_HISTORY' => 'counter_history',
			'TABLE_COUNTRIES' => 'countries',
			'TABLE_COUPON_GV_QUEUE' => 'coupon_gv_queue',
			'TABLE_COUPON_GV_CUSTOMER' => 'coupon_gv_customer',
			'TABLE_COUPON_EMAIL_TRACK' => 'coupon_email_track',
			'TABLE_COUPON_REDEEM_TRACK' => 'coupon_redeem_track',
			'TABLE_COUPON_RESTRICT' => 'coupon_restrict',
			'TABLE_COUPONS' => 'coupons',
			'TABLE_COUPONS_DESCRIPTION' => 'coupons_description',
			'TABLE_CURRENCIES' => 'currencies',
			'TABLE_CUSTOMERS' => 'customers',
			'TABLE_CUSTOMERS_BASKET' => 'customers_basket',
			'TABLE_CUSTOMERS_BASKET_ATTRIBUTES' => 'customers_basket_attributes',
			'TABLE_CUSTOMERS_INFO' => 'customers_info',
			'TABLE_DB_CACHE' => 'db_cache',
			'TABLE_EMAIL_ARCHIVE' => 'email_archive',
			'TABLE_EZPAGES' => 'ezpages',
			'TABLE_EZPAGES_CONTENT' => 'ezpages_content',
			'TABLE_FEATURED' => 'featured',
			'TABLE_FILES_UPLOADED' => 'files_uploaded',
			'TABLE_GROUP_PRICING' => 'group_pricing',
			'TABLE_GET_TERMS_TO_FILTER' => 'get_terms_to_filter',
			'TABLE_LANGUAGES' => 'languages',
			'TABLE_LAYOUT_BOXES' => 'layout_boxes',
			'TABLE_MANUFACTURERS' => 'manufacturers',
			'TABLE_MANUFACTURERS_INFO' => 'manufacturers_info',
			'TABLE_META_TAGS_PRODUCTS_DESCRIPTION' => 'meta_tags_products_description',
			'TABLE_METATAGS_CATEGORIES_DESCRIPTION' => 'meta_tags_categories_description',
			'TABLE_NEWSLETTERS' => 'newsletters',
			'TABLE_ORDERS' => 'orders',
			'TABLE_ORDERS_PRODUCTS' => 'orders_products',
			'TABLE_ORDERS_PRODUCTS_ATTRIBUTES' => 'orders_products_attributes',
			'TABLE_ORDERS_PRODUCTS_DOWNLOAD' => 'orders_products_download',
			'TABLE_ORDERS_STATUS' => 'orders_status',
			'TABLE_ORDERS_STATUS_HISTORY' => 'orders_status_history',
			'TABLE_ORDERS_TYPE' => 'orders_type',
			'TABLE_ORDERS_TOTAL' => 'orders_total',
			'TABLE_PAYPAL' => 'paypal',
			'TABLE_PAYPAL_SESSION' => 'paypal_session',
			'TABLE_PAYPAL_PAYMENT_STATUS' => 'paypal_payment_status',
			'TABLE_PAYPAL_PAYMENT_STATUS_HISTORY' => 'paypal_payment_status_history',
			'TABLE_PAYPAL_TESTING' => 'paypal_testing',
			'TABLE_PRODUCTS' => 'products',
			'TABLE_PRODUCT_TYPES' => 'product_types',
			'TABLE_PRODUCT_TYPE_LAYOUT' => 'product_type_layout',
			'TABLE_PRODUCT_TYPES_TO_CATEGORY' => 'product_types_to_category',
			'TABLE_PRODUCTS_ATTRIBUTES' => 'products_attributes',
			'TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD' => 'products_attributes_download',
			'TABLE_PRODUCTS_DESCRIPTION' => 'products_description',
			'TABLE_PRODUCTS_DISCOUNT_QUANTITY' => 'products_discount_quantity',
			'TABLE_PRODUCTS_NOTIFICATIONS' => 'products_notifications',
			'TABLE_PRODUCTS_OPTIONS' => 'products_options',
			'TABLE_PRODUCTS_OPTIONS_VALUES' => 'products_options_values',
			'TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS' => 'products_options_values_to_products_options',
			'TABLE_PRODUCTS_OPTIONS_TYPES' => 'products_options_types',
			'TABLE_PRODUCTS_TO_CATEGORIES' => 'products_to_categories',
			'TABLE_PROJECT_VERSION' => 'project_version',
			'TABLE_PROJECT_VERSION_HISTORY' => 'project_version_history',
			'TABLE_QUERY_BUILDER' => 'query_builder',
			'TABLE_REVIEWS' => 'reviews',
			'TABLE_REVIEWS_DESCRIPTION' => 'reviews_description',
			'TABLE_SALEMAKER_SALES' => 'salemaker_sales',
			'TABLE_SESSIONS' => 'sessions',
			'TABLE_SPECIALS' => 'specials',
			'TABLE_TEMPLATE_SELECT' => 'template_select',
			'TABLE_TAX_CLASS' => 'tax_class',
			'TABLE_TAX_RATES' => 'tax_rates',
			'TABLE_GEO_ZONES' => 'geo_zones',
			'TABLE_ZONES_TO_GEO_ZONES' => 'zones_to_geo_zones',
			'TABLE_UPGRADE_EXCEPTIONS' => 'upgrade_exceptions',
			'TABLE_WISHLIST' => 'customers_wishlist',
			'TABLE_WHOS_ONLINE' => 'whos_online',
			'TABLE_ZONES' => 'zones'
		);
	}
	
	private function tableName($table) {
		return $this->database_tables['TABLE_PREFIX'] . $this->database_tables[strtoupper($table)];
	}
	
	static public function getInstance() {
		if(is_null(self::$_instance)) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	
	public function get() {
		$parameters = func_get_args();
		$key = $parameters[0];
		
		// Check the database_settings
		if (substr($key, 0, 3) == strtoupper("DB_")) {
			return $this->database_settings[strtoupper( array_shift( $parameters) )];

		// Check the pos_settings
		} else if (substr($key, 0, 4) == strtoupper("POS_")) {
			return $this->pos_settings[strtoupper( array_shift( $parameters) )];
		
		// Check the database_tables
		} else if (substr($key, 0, 6) == strtoupper("TABLE_")) {
			return $this->tableName($key);
			
		} else if (array_key_exists($key, $this->cache)) {
			return $this->cache[$key];
			
		// Check the configuration table in the Zen Cart database
		} else {
			
			$query = 	"SELECT cf.configuration_value
						FROM ". $this->tableName('TABLE_CONFIGURATION') ." cf
						WHERE configuration_key = '" . $key . "'";
			
			$db_value = $this->db->execute($query)->fields['configuration_value'];
			
			if (isset($db_value)) {
				$this->cache[$key] = $db_value;
				return $db_value;
			}else{
				throw new Exception("Configuration value for ".$key." not found");
			}
		}
	}
	
	public function set($key, $value) {
		// Check the database_settings
		if (substr($key, 0, 3) == strtoupper("DB_")) {
			if (array_key_exists($key, $this->database_settings)) {
				$this->database_settings[$key] = $value;
			} else return false;

		// Check the pos_settings
		} else if (substr($key, 0, 4) == strtoupper("POS_")) {
			if (array_key_exists($key, $this->pos_settings)) {
				$this->pos_settings[$key] = $value;
			} else return false;
		
		// Check the database_tables
		} else if (substr($key, 0, 6) == strtoupper("TABLE_")) {
			if (array_key_exists($key, $this->database_tables)) {
				$this->database_tables[$key] = $value;
			} else return false;
		
		} else {
			return false;
		}
		
	}
	
	public function connect() {
		/* Allow the this to be called from outside the class if DB Settings Change during runtime */
		$this->db->connect($this->database_settings['DB_SERVER'], $this->database_settings['DB_SERVER_USERNAME'], $this->database_settings['DB_SERVER_PASSWORD'], $this->database_settings['DB_DATABASE'], true, false);
	}
	
	public function hasService( $service ) {
		return false;
	}
}
?>